/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import java.awt.image.BufferedImage;
import java.io.IOException;
import net.worlds.AssetManager;
import net.worlds.WorldsException;
import net.worlds.PropChangeHandler;
import org.jbox2d.common.Vec2D;


public class NPC extends AbstractCharacter{
    private BufferedImage toolTexture = null;
    private Vec2D toolSize = null;
    public NPC() throws WorldsException
    {
        super("NPC");
        addPropChangeHandler("ToolTexture", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(value.toString().equals(""))
                    toolTexture = null;
                else
                    try {
                        toolTexture = AssetManager.loadImage(value.toString(), value.toString());
                        toolSize = new Vec2D(toolTexture.getWidth(), toolTexture.getHeight()).div(8);
                    } catch (IOException ex) {
                        toolTexture = null;
                    }
            }
        });
    }
    @Override
    public void addToWorld() throws WorldsException
    {
        super.addToWorld();
    }
    @Override
    protected double getArmAngle() throws WorldsException {
        return Math.toRadians(-getFloatProp("ArmRotation"));
    }

    @Override
    protected BufferedImage getToolTexture() throws WorldsException {
        return toolTexture;
    }

    @Override
    protected Vec2D getToolSize() throws WorldsException {
        return toolSize;
    }

    @Override
    protected Vec2D getToolOffset() throws WorldsException {
        return getVec2DProp("ToolOffset");
    }

    @Override
    protected boolean armRotationEnabled() throws WorldsException {
        return toolTexture != null;
    }

    @Override
    public boolean isPlayer() throws WorldsException {
        return false;
    }

    @Override
    protected Vec2D getPositionDifference() {
        try {
            if(hasChild("Body") && getChild("Body") instanceof Part)
            {
                Part bodyPart = (Part) getChild("Body");
                return bodyPart.getPosition().sub(bodyPart.getOldPosition());
            }
            else
                return Vec2D.ORIGIN;
        } catch (WorldsException ex) {
            return Vec2D.ORIGIN;
        }
    }

}
